<template>
  <div class="container" style="height: 100vh;width: 300px;padding: 20px;box-sizing: border-box;width: 100%;">
    <header>
      <MBreadcrumb :item="breadcrumbItem" />
    </header>
    <main>
      <MTitle class="title">
        <template #left>
          出库单
        </template>
      </MTitle>
      <table id="attributeTable" class="mtable">
        <tr class="mtr">
          <td class="field">收货单位</td>
          <td class="value">
            <MInput v-model="ProductInfo[0]" style="font-size: 12px;width: 100%" disabled></MInput>
          </td>
        </tr>
        <tr class="mtr">
          <td class="field">物品信息</td>
          <td class="value">
            <MInput v-model="ProductInfo[1]" style="font-size: 12px;width: 100%" disabled></MInput>
          </td>
        </tr>
        <tr class="mtr">
          <td class="field">数量</td>
          <td class="value">
            <MInput v-model="ProductInfo[2]" style="font-size: 12px;width: 100%" disabled></MInput>
          </td>
        </tr>
        <tr class="mtr">
          <td class="field">已出库</td>
          <td class="value">
            <MInput v-model="ProductInfo[3]" style="font-size: 12px;width: 100%" disabled></MInput>
          </td>
        </tr>

      </table>
      <MTitle class="title">
        <template #left>
          包装信息
        </template>
      </MTitle>
      <table id="attributeTable" class="mtable">
        <tr class="mtr">
          <td class="field">包装型号</td>
          <td class="value">
            <MInput v-model="ProductInfo[4]" style="font-size: 12px;width: 100%" disabled></MInput>
          </td>
        </tr>
        <tr class="mtr">
          <td class="field">使用产品</td>
          <td class="value">
            <MInput v-model="ProductInfo[1]" style="font-size: 12px;width: 100%" disabled></MInput>
          </td>
        </tr>
        <tr class="mtr">
          <td class="field">包装名称</td>
          <td class="value">
            <MInput v-model="ProductInfo[5]" style="font-size: 12px;width: 100%" disabled></MInput>
          </td>
        </tr>
        <tr class="mtr">
          <td class="field">包装数量</td>
          <td class="value">
            <MInput v-model="ProductInfo[6]" style="font-size: 12px;width: 100%" disabled></MInput>
          </td>
        </tr>

      </table>
      <MTitle class="title">
        <template #left>
          标识信息
        </template>
      </MTitle>
      <MTag v-for="(obj, index) in tagItems" :key="index">{{ obj }}</MTag>
    </main>
    <footer v-if="!isDisable">
      <MButton @click="printOrder" v-if="!isPrint">打印出库单</MButton>
      <MButton @click="transmitLogistics" v-if="isPrint">转交物流员</MButton>
    </footer>
    <MMessage v-if="toastVisible" :message="toastMessage" :state="toastState" />

  </div>
</template>

<script>
import { ref, onMounted } from 'vue';
import MBreadcrumb from "../../components/MBreadcrumb.vue"
import MTitle from "../../components/MTitle.vue"
//import MList from "../../components/MList.vue"
import MButton from "../../components/MButton.vue"
import MInput from "../../components/MInput.vue"
import MMessage from '../../components/MMessage.vue';
import MTag from '../../components/MTag.vue';
import axios from "../../axios/axios"
export default {
  name: "PackageInfo",
  components: {
    MBreadcrumb,
    MTitle,
    //MSelect,
    MButton,
    MInput,
    MMessage,
    MTag
    //MIcon
  },
  setup() {
    const toastVisible = ref(false)
    const toastMessage = ref("")
    const toastState = ref("success")
    const isPrint = ref(false)
    const breadcrumbItem = ref(
      {
        key: "0",
        title: '赋码管理',
        children: {
          key: "0-0",
          title: '出库信息',
        }
      })
    //const userId="12121"
    const ProductInfo = ref(["", "", "", "0", "KVSP750110", "75寸转运木箱01型", "10"])
    const identifys = ref([])
    const tagItems = ref([])
    const params = ref([])
    const userId = ref("")
    const orderInfo = ref({})
    const isDisable = ref(false)

    const printOrder = () => {
      if (ProductInfo.value[3] === ProductInfo.value[2]) {
        axios.get("/step/sendstep?type=printOrder&userId=" + params.value.get('userId') + "&trainingId=" + params.value.get('trainingId')).then(res => {
          if (res.data.Code === "200") {
            messageShow("已打印", "success")
            isPrint.value = true
          }
        })
      } else {
        messageShow("请完成出库", "warning")
      }

    }
    const transmitLogistics = () => {
      axios.get("/step/sendstep?type=transmitLogistics&userId=" + params.value.get('userId') + "&trainingId=" + params.value.get('trainingId')).then(res => {
        if (res.data.Code === "200") {
          messageShow("已转交", "success")
        }
      })
    }

    const messageShow = (msg, state) => {
      toastMessage.value = msg;
      toastVisible.value = true;
      toastState.value = state
      // 使用 setTimeout 模拟 Toast 组件内部的定时器功能（仅用于演示）
      // 在实际使用中，你应该依赖 Toast 组件内部的定时器
      setTimeout(() => {
        toastVisible.value = false;
      }, 3000); // 与 Toast 组件中的 duration 属性保持一致
    }

    onMounted(() => {
      // 获取 URL 参数
      const param = new URLSearchParams(window.location.search);
      params.value = param
      if (param.has('userId')) {
        userId.value = param.get('userId');
      }
      if (param.has('isDisable')) {
        isDisable.value = param.get('isDisable') === "true"
      }
      var isPart = "1"
      axios.get('/order/select?userId=' + param.get('userId') + "&trainingId=" + param.get('trainingId') + "&isPart=" + isPart)
        .then(res => {
          var data = res.data.Data
          orderInfo.value = data
          var info = [...ProductInfo.value]
          info[0] = data.companyname
          info[1] = data.productmodel
          info[2] = data.goodsnumber
          ProductInfo.value = info
          axios.get('/logistics/select?orderNumber=' + res.data.Data.ordernumber)
            .then(res1 => {
              axios.get('/delivery/select?logisticsNumber=' + res1.data.Data.logisticsnumber)
                .then(res2 => {
                  if (res2.data.Code === "200") {
                    var data1 = res2.data.Data
                    var info = [...ProductInfo.value]
                    info[3] = data1.outnumber
                    info[4] = data1.packmodel
                    info[5] = data1.packname
                    info[6] = data1.packnumber
                    ProductInfo.value = info
                  }

                })
            })
        })
      if (param.get('code') !== null) {
        // getInfo(param)

        const dataList = param.get('code').split(",")
        tagItems.value = dataList
      }
    });
    return {
      toastVisible,
      toastMessage,
      toastState,
      breadcrumbItem,
      tagItems,
      ProductInfo,
      identifys,
      isDisable,
      printOrder,
      transmitLogistics,
      isPrint
    };
  },
};
</script>

<style scoped>
.title {
  margin-top: 30px;
}

.title1 {
  margin-top: 20px;
}

.mtr {
  display: flex;
  flex-wrap: wrap;
  /* 允许子元素换行 */
  align-items: flex-start;
}

.container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

footer {
  text-align: center;
  height: 10%;
}

main {
  flex: 1;
}

.mtable {
  width: 100%;
  font-size: 13px;

}

.field {
  text-align: left;
  padding: 4px 0;
  flex: 1 1 60px;
  word-break: break-word;
  /* 允许长单词换行 */
  overflow-wrap: break-word;
  min-width: 0;
  /* 确保内容可以缩小 */
  height: 26px;
  line-height: 26px;
}

.value {
  text-align: right;
  padding: 4px 0;
  flex: 1 1 170px;
  word-break: break-word;
  /* 允许长单词换行 */
  overflow-wrap: break-word;
  min-width: 0;
  height: 26px;
  line-height: 26px;
  /* 确保内容可以缩小 */
}
</style>
